home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / LIB / PRESS_LA.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  5.7 KB  |  191 lines

  1. package sub_arctic.lib;
  2.  
  3. import sub_arctic.input.*;
  4. import sub_arctic.output.*;
  5. import sub_arctic.constraints.constraint;
  6.  
  7. import java.awt.Font;
  8. import java.awt.FontMetrics;
  9.  
  10. /**
  11.  * This is just a little object which makes labels have a callback and
  12.  * keep track of their state. Its useful in things like menus and what
  13.  * not when someone else needs to stay informed of state changes. 
  14.  * This object sends a callback to its callback object with
  15.  * a boolean as the info indicating its new state.<p>
  16.  *
  17.  * This object allows its width to be anything the parent (or anyone
  18.  * else) wishes (unlike more traditional labels). 
  19.  *
  20.  * @author Ian Smith
  21.  */
  22. public class press_label extends label implements pressable {
  23.  
  24.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  25.  
  26.   /**
  27.    * Keep track of the active state. 
  28.    */
  29.   protected  boolean _active;
  30.  
  31.   /**
  32.    * Return whether the object is active or not. 
  33.    * @return int true if the object is active
  34.    */
  35.   public boolean active() { return _active;};
  36.  
  37.   /**
  38.    * Make the active state the opposite of what it is now.
  39.    */
  40.   public void invert_active() {
  41.     /* invert the state of active */
  42.     if (active()) {
  43.       _active=false;
  44.     } else {
  45.       _active=true;
  46.     }
  47.     /* invert the display */
  48.     set_draw_colors(draw_colors().inverted());
  49.   }
  50.  
  51.   /**
  52.    * Set the active state to either true or false.
  53.    * @param boolean b the new active state 
  54.    */
  55.   public void set_active(boolean b) {
  56.     if (active()!=b) {
  57.       invert_active();
  58.     }
  59.   }
  60.  
  61.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  62.  
  63.   /**
  64.    * The object to whom we are sending our callback.
  65.    */
  66.   protected callback_object _callback_obj;
  67.  
  68.   /**
  69.    * Access the current callback object.
  70.    * @return callback_object the object to whom we are sending callbacks.
  71.    */
  72.   public callback_object callback_obj() { return _callback_obj;};
  73.  
  74.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  75.  
  76.  
  77.   /**
  78.    * Construct a new press_label.
  79.    * @param String          s       the string to display in the label.
  80.    * @param boolean         opacity true if the object should be opaque (paint 
  81.    *                                its background).
  82.    * @param callback_object obj     the object to send the callback's to.
  83.    */
  84.   public press_label(String s, Font f, boolean opacity, callback_object obj) {
  85.  
  86.     super(s,f);
  87.  
  88.     /* do we want opacity */
  89.     set_opaque(opacity);
  90.  
  91.     /* initially we are not active */
  92.     _active=false;
  93.  
  94.     /* set the callback object to the passed in one */
  95.     _callback_obj=obj;
  96.   }
  97.  
  98.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  99.  
  100.   /** Callback number constant for callbeack when we are pressed. */
  101.   public static final int LABEL_ACTION_CALLBACK = 0;
  102.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  103.  
  104.   /**
  105.    * Respond to the press method from the agent.  This object generates
  106.    * its callback on the <I>press</I> not on a matched pair of 
  107.    * press and release.
  108.    * 
  109.    * @param event  evt       the press event (usually mouse down).
  110.    * @param Object user_info the object passed the pick_collector at pick-time.
  111.    * @return boolean true if we consumed the event
  112.    */
  113.   public boolean press(event evt, Object user_info) {
  114.  
  115.     /* invert our state */
  116.     invert_active();
  117.  
  118.     if (callback_obj()!=null) {
  119.       /* just tell the other code what happened */
  120.       callback_obj().callback(this,evt,LABEL_ACTION_CALLBACK,
  121.                               new Boolean(active()));
  122.     }
  123.  
  124.     /* we took it */
  125.     return true;
  126.   }
  127.  
  128.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  129.  
  130.   /**
  131.    * Respond to the release method from the agent. 
  132.    * @param event  evt       the press event (usually mouse up).
  133.    * @param Object user_info the object passed the pick_collector at pick-time.
  134.    * @return boolean true if we consumed the event
  135.    */
  136.   public boolean release(event evt, Object user_info) {
  137.     return false;
  138.   }
  139.  
  140.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  141.  
  142.   /**
  143.    * Respond to questions about how wide our string is.
  144.    * @return int the number of pixels wide the string is (plus twice the 
  145.    *             horizontal spacing to take into account the borders on left 
  146.    *             and right).
  147.    */
  148.   public int string_width() { 
  149.     String s=text();
  150.     FontMetrics metrics=manager.get_metrics(font());
  151.  
  152.     /* ok compute the string size + 2 times the border */
  153.     int w=metrics.stringWidth(s)+(2*_h_spacing);
  154.  
  155.     return w;
  156.   }
  157.  
  158.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  159.  
  160.   /**
  161.    * Fake out our superclass with a new implementation of 
  162.    * intrinsic constraints which returns only a height constraint.
  163.    * We don't have a intrinsic width because we are going to be
  164.    * flexible about putting ourselves in any width requested
  165.    * by the parent.
  166.    * 
  167.    * @return int the constant for an intrinsic constraint on width
  168.    */
  169.   public int intrinsic_constraints() {
  170.     return interactor_consts.H; /* we have an intrinsic height but not width */
  171.   }
  172.  
  173.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  174. }
  175. /*=========================== COPYRIGHT NOTICE ===========================
  176.  
  177. This file is part of the subArctic user interface toolkit.
  178.  
  179. Copyright (c) 1996 Scott Hudson and Ian Smith
  180. All rights reserved.
  181.  
  182. The subArctic system is freely available for most uses under the terms
  183. and conditions described in 
  184.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  185. and appearing in full in the lib/interactor.java source file.
  186.  
  187. The current release and additional information about this software can be 
  188. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  189.  
  190. ========================================================================*/
  191.